package com.ch12;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;

/**
 * 练习10 在不知道行数的情况下随机的输出一行
 * 
 * @author Linxiaozhi
 * 
 */
public class Test10 {

	static int f1(BufferedReader br) {
		int select = 1;
		Random rand = new Random(System.currentTimeMillis());
		try {
			int counter = 1;
			while (br.readLine() != null) {
				if (rand.nextInt(counter) < 1) {
					select = counter;
				}
				counter++;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return select;
	}

	public static void main(String[] args) {
		try {
			BufferedReader br = new BufferedReader(new FileReader("d:\\d.txt"));
			System.out.println(f1(br));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}
